#!/usr/bin/python

from pylab import *

x1 = linspace(0, pi*2/3, 100, endpoint=True)
y = sin(x1)
x2 = linspace(pi, pi*3/2, 100, endpoint=True)
z = cos(x2)
x3 = linspace(0, pi*5/2/4, 100, endpoint=True) + 2
w = sin(4*(x3-2))/3
# x4 = linspace(0, pi, 100, endpoint=True)
x4 = x1
v = cos(x4)/2 - 1

plot(x1, y, 'r', linewidth=4)
plot(x2, z, 'r', linewidth=4)
plot(x3, w, 'r', linewidth=4)
plot(x4, v, 'r', linewidth=4)

# fill(x1,y, 'b')
fill(x2,z, 'g')
fill(x3, w, 'y')
# # fill(x1,y,'b', x2,z,'g')

fill_between(x1, y, y2=-0.5)
# fill_between(x1, y, y2=0.5, interpolate=True)
# fill_between(x1, y, y2=0.5, where=x1>pi/2, interpolate=True)
# fill_between(x1, y, y2=v, interpolate=True)
# fill_between(x1, y, y2=v, interpolate=True)

# grid()
# show()


xa = arange(0, 2, 0.01)
y1 = sin(2*pi*xa)
y2 = 1.2*sin(4*pi*xa)

fig = figure(figsize=(8,6), dpi=80)
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212, sharex=ax1)

# ax1.plot(xa, y1, 'b', xa, y2, 'g', linewidth=2)
ax1.plot(xa, y1, 'b', label='$sin(2\pi{x})$', linewidth=2)
ax1.plot(xa, y2, 'g', label='$1.2sin(4\pi{x})$', linewidth=2)
ax1.fill_between(xa, y1, y2, where=y2>=y1, facecolor='red', alpha=0.2, interpolate=True) # color consists of facecolor and edgecolor
ax1.fill_between(xa, y1, y2, where=y2<y1, facecolor='yellow', interpolate=True)

ax1.legend(loc="upper left")

grid()
show()
